using System;
using Habanero.Base;
using Habanero.Base.Exceptions;

namespace FireStarterModeller.BO
{
    /// <summary>
    /// A <see cref="IBusinessObjectRule"/> that validates that a <see cref="DMClass"/>
    /// has one <see cref="UIView"/> defined
    /// </summary>
    public class DMLookupListSortOrderPropsMustExist : IBusinessObjectRule
    {
        private DMLookupListBusinessObject _dmLookupList;

//        public DMLookupListSortOrderPropsMustExist(DMLookupListBusinessObject dmLookupList)
//        {
//            if (dmLookupList == null) throw new ArgumentNullException("dmLookupList");
//            _dmLookupList = dmLookupList;
//        }

        #region Implementation of IBusinessObjectRule

        /// <summary>
        /// Indicates whether the property value is valid against the rules
        /// </summary>
        /// <returns>
        /// Returns true if valid
        /// </returns>
        public bool IsValid()
        {
            throw new HabaneroApplicationException("The IsValid is no longer Valid you should use the IsValid(bo) method");
        }

        public bool IsValid(IBusinessObject bo)
        {
            _dmLookupList = (DMLookupListBusinessObject) bo;
            if (this._dmLookupList.DMSortOrder == null) return true;
            if (this._dmLookupList.DMClass == null) return true;
            foreach (DMSortProperty sortProperty in this._dmLookupList.DMSortOrder.DMSortProperties)
            {
                DMSortProperty dmSortProperty = sortProperty;
                DMProperty foundProp = this._dmLookupList.DMClass.Properties.Find(property => property.PropertyName == dmSortProperty.PropertyName);
                if (foundProp == null) return false;
            }
            return true;
        }

        /// <summary>
        /// Returns the rule name
        /// </summary>
        public string Name
        {
            get { return "DMLookupListSortOrderPropsMustExist"; }
        }

        /// <summary>
        /// Returns the error message for if the rule fails.
        /// </summary>
        public string Message
        {
            get { return "The LookupList '" + _dmLookupList + "' for property '" + _dmLookupList.DMProperty.PropertyName 
                    + " for Business Object '" + _dmLookupList.DMClass 
                    + "' cannot have a properties of its sort order that do not exist in the LookupList's Business Object"; }
        }
        /// <summary>
        /// The <see cref="P:Habanero.Base.IBusinessObjectRule.ErrorLevel" /> for this BusinessObjectRule e.g. Warning, Error. 
        /// </summary>
        public ErrorLevel ErrorLevel
        {
            get { return Habanero.Base.ErrorLevel.Warning; }
        }

        #endregion
    }
}